<?php

namespace app\admin\controller\promotion;

use app\admin\model\UM as UM;
use app\admin\service\GroupRoiConsumeInfoService;
use ShidianUtil\Thinkphp\Response\ApiResponse;
use ShidianUtil\Utils\TimeUtil;
use think\APP;
use think\exception\ValidateException;
use think\Request as Request;

/**
 * GroupRoiConsumeInfo 控制器
 * Class GroupRoiConsumeInfo
 * @package app\admin\controller\promotion;
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037509
 * @see \app\admin\validate\promotion\GroupRoiConsumeInfo
 */
class GroupRoiConsumeInfo
{
    // 请求参数
    private Request $request;

    // service
    private GroupRoiConsumeInfoService $service;

    public function __construct(Request $request, GroupRoiConsumeInfoService $groupRoiConsumeInfoService)
    {
        $this->request = $request;
        $this->service = $groupRoiConsumeInfoService;
    }

    /**
     * 列表
     * @param UM|null $user
     * @return ApiResponse
     * @url /promotion/group_roi_consume_info/index
     */
    public function index(?UM $user): ApiResponse
    {
        $request = $this->request;

        $promotionGroupId = $request->param('promotionGroupId');
        if(!is_id($promotionGroupId)){
            throw new ValidateException('请选择一个小组');
        }

        $time = $request->param('time', time());
        if(!is_numeric($time)){
            throw new ValidateException('请选择时间');
        }
        $time = TimeUtil::stamp(TimeUtil::MONTH, $time);

        return ApiResponse::success($this->service->index($user, $promotionGroupId, $time));
    }

    /**
     * 新增|修改
     * @param UM $user
     * @return ApiResponse
     * @url /promotion/group_roi_consume_info/save
     */
    public function save(UM $user): ApiResponse
    {
        $request = $this->request;

        $this->service->add($user, $request->param());

        return ApiResponse::success(null);
    }

}
